﻿Public Class ucCategory
    Dim mCatID As Integer
    Dim mCategoryItem As CategoryItem
    Dim mCategory As Category
    Private Sub ucCategory_Load(sender As Object, e As EventArgs) Handles Me.Load
        mCategory = New Category
        LoadCategories()
        ResetFields()
    End Sub
#Region "Loading"
    Sub LoadCategories()
        Dim itm As ListViewItem
        'Reset menu
        lvwCategories.Groups.Clear()
        lvwCategories.Items.Clear()
        'Add items
        Dim a = (From x In gCat.CategoryList Order By x.CategoryDesc Descending).ToList
        For Each item In a
            itm = New ListViewItem With {.ImageIndex = 0, .Text = item.CategoryDesc, .Tag = item.CategoryID}
            lvwCategories.Items.Add(itm)
        Next
    End Sub
#End Region
#Region "Process UI"
    Private Sub lvwCategories_Click(sender As Object, e As EventArgs) Handles lvwCategories.Click
        If lvwCategories.SelectedItems.Count > 0 Then
            mCatID = CInt(lvwCategories.SelectedItems(0).Tag)
            mCategoryItem = gCat.GetCategory(mCatID)
            txtCategory.Text = mCategoryItem.CategoryDesc
            btnAdd.Text = "Update"
        Else
            ResetFields()
        End If
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        If mCatID = 0 Then
            mCategoryItem = New CategoryItem
        Else
            mCategoryItem = gCat.GetCategory(mCatID)
        End If
        mCategoryItem.CategoryDesc = txtCategory.Text.Trim
        mCatID = mCategory.Save(mCategoryItem)
        If mCatID <> 0 Then gCat = New Category
        ResetFields()
        LoadCategories()
    End Sub
#End Region
#Region "General Functions"
    Sub ResetFields()
        txtCategory.Text = ""
        btnAdd.Text = "Add"
        mCatID = 0
    End Sub
#End Region
End Class
